/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.com.customer.manager.http;

import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.annotation.NetMethod;
import com.xuexiang.xhttp2.callback.DownloadProgressCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.model.ApiResult;
import com.xuexiang.xutil.app.PathUtils;
import com.xuexiang.xutil.display.HProgressDialogUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.List;
import java.util.Map;

import cn.com.customer.manager.R;
import cn.com.customer.manager.bean.AddOrderInfo;
import cn.com.customer.manager.bean.BelongUserInfo;
import cn.com.customer.manager.bean.CallList;
import cn.com.customer.manager.bean.CustomerInfo;
import cn.com.customer.manager.bean.CustomerReiveInfo;
import cn.com.customer.manager.bean.EmailInfo;
import cn.com.customer.manager.bean.EmailListInfo;
import cn.com.customer.manager.bean.GoodListInfo;
import cn.com.customer.manager.bean.GoodsList;
import cn.com.customer.manager.bean.GroupResult;
import cn.com.customer.manager.bean.MessageListInfo;
import cn.com.customer.manager.bean.MsgListInfo;
import cn.com.customer.manager.bean.NewStatusList;
import cn.com.customer.manager.bean.OrderDetailInfo;
import cn.com.customer.manager.bean.OrderListInfo;
import cn.com.customer.manager.bean.PayWayResult;
import cn.com.customer.manager.bean.ReceivePayListInfo;
import cn.com.customer.manager.bean.RemindWayResult;
import cn.com.customer.manager.bean.RequestBean;
import cn.com.customer.manager.bean.RequestOrder;
import cn.com.customer.manager.bean.SendMsgInfo;
import cn.com.customer.manager.bean.StatusList;
import cn.com.customer.manager.bean.TagsInfo;
import cn.com.customer.manager.bean.TaskAddInfo;
import cn.com.customer.manager.bean.TaskInfo;
import cn.com.customer.manager.bean.TaskListRespond;
import cn.com.customer.manager.bean.TokenResult;
import cn.com.customer.manager.bean.TypeResult;
import cn.com.customer.manager.bean.UpdateInfo;
import cn.com.customer.manager.bean.User;
import cn.com.customer.manager.bean.UserList;
import io.reactivex.Observable;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

import static cn.com.customer.manager.base.BaseApplication.getContext;
import static com.xuexiang.xutil.resource.ResUtils.getString;

/**
 * api协议
 *
 * @author wjy
 */
public class BaseApi {


    /**
     * 身份验证
     */
    public interface IAuthorization {
        /**
         * 登录获取token
         *
         * @param loginName 用户名
         * @param password  密码
         */
        @NetMethod(parameterNames = {"username", "password"}, url = "usr/login", accessToken = false)
        Observable<TokenResult> login(String loginName, String password);

        /**
         * @param
         * @return
         * @method 退出登录
         * @description 描述一下方法的作用
         * @date: 2020/6/21 10:52
         * @author: wjy
         */
        @NetMethod(parameterNames = {"token"}, url = "usr/logout")
        Observable<ApiResult<String>> loginOut(String token);

        /**
         * @param
         * @return
         * @method 获取用户信息
         * @description 描述一下方法的作用
         * @date: 2020/6/21 10:56
         * @author:wjy
         */
        @NetMethod(parameterNames = {"token"}, url = "usr/info")
        Observable<User> userInfo(String token);

        /**
         * @param
         * @return
         * @method 跟新用户信息
         * @description 描述一下方法的作用
         * @date: 2020/6/21 11:04
         * @author: wjy
         */
        @NetMethod(parameterNames = {"token,data"}, url = "usr/modify")
        Observable<Map<String,String>> modifyUser(@Body RequestBean<User> requestBean);

        /**
         * @param
         * @return
         * @method 更新密码
         * @description 描述一下方法的作用
         * @date: 2020/6/21 11:24
         * @author: wjy
         */
        @POST("usr/modifyPassword")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<Map<String,String>> modifyPassword(@Body RequestBean<Map<String, String>> requestBean);
        /**
         * @param
         * @return
         * @method 版本升级
         * @description 描述一下方法的作用
         * @date: 2020/6/21 11:24
         * @author: wjy
         */
        @POST("sys/checkUpdate")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<UpdateInfo> update(@Body RequestBean<Map<String, Object>> requestBean);

        @POST("usr/usrList")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<List<UserList>>> userList(@Body RequestBean<Map<String, String>> requestBean);
    }

    public interface IMessage {
        /**
         * @param : {
         *          "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "pageNo":"1",
         *          "pageSize":"10",
         *          "noticeTitle":""
         *          }
         *          }
         * @return
         * @method
         * @description 3.1查询通知列表
         * @date: 2020/6/26 16:39
         * @author: wjy
         */
        @POST("notice/list")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<MessageListInfo>> noticeList(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "noticeId":"1432135435"
         *          }
         *          }
         * @return
         * @method 3.2更新通知状态
         * @description 描述一下方法的作用
         * @date: 2020/6/26 16:51
         * @author: wjy
         */
        @POST("notice/modifyStatus")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<Map<String, String>> noticeModifyStatus(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @return
         * @method 3.3查询短信列表
         * @description 描述一下方法的作用
         * @date: 2020/6/26 16:55
         * @author: wjy
         * @param{ "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         * "data":{
         * "pageNo":"1",
         * "pageSize":"10",
         * "beginDate":"2020-01-01",
         * "endDate":"2020-01-03"
         * }
         * }
         */
        @POST("sms/list")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<MsgListInfo>> smsList(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "smsId":"1432135435"
         *          }
         *          }
         * @return ""
         * @method 3.4更新短信状态
         * @description 描述一下方法的作用
         * @date: 2020/6/26 17:12
         * @author: wjy
         */
        @POST("sms/modifyStatus")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<Map<String, String>> smsModifyStatus(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "receiverList":[
         *          {
         *          "mobile":"13678680941"
         *          },
         *          {
         *          "mobile":"13678680942"
         *          }
         *          ],
         *          "smsContent":"您订购的货物已到达，请您尽快安排提货。"
         *          }
         *          }
         * @return ""
         * @method 3.5短信回复与发送
         * @description 描述一下方法的作用
         * @date: 2020/6/26 17:13
         * @author: wjy
         */
        @POST("sms/send")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<Map<String, String>> smsSend(@Body RequestBean<SendMsgInfo> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":""
         *          }
         * @return
         * @method 3.6查询邮件列表
         * @description 描述一下方法的作用
         * @date: 2020/6/26 17:26
         * @author: wjy
         */
        @POST("mail/inboxList")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<EmailListInfo>> mailInboxList(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "mailId":"12312423432"
         *          }
         *          }
         * @return
         * @method 3.7查询邮件详情
         * @description 描述一下方法的作用
         * @date: 2020/6/26 17:34
         * @author: wjy
         */
        @POST("mail/detail")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<EmailInfo>> mailDetail(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "pageNo":"1",
         *          "pageSize":"10",
         *          "beginDate":"2020-01-01",
         *          "endDate":"2020-01-03"
         *          }
         *          }
         * @return
         * @method 3.8查询通话记录
         * @description 描述一下方法的作用
         * @date: 2020/6/26 17:53
         * @author: wjy
         */
        @POST("call/list")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<CallList>> callList(@Body RequestBean<Map<String, String>> requestBean);


    }

    /**
     * 通话
     */
    public interface ICommunication {
        @POST("customer/queryByMobile")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<CustomerInfo>> queryByMobile(@Body RequestBean<Map<String, String>> requestBean);

    }

    public interface ITask {
        /**
         * @param : pageNo pageSize taskType(任务类型) customerName（客户名称）
         * @return
         * @method 任务列表
         * @description 描述一下方法的作用
         * @date: 2020/6/21 15:09
         * @author: wjy
         */
        @POST("task/list")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<TaskListRespond>> taskList(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @return
         * @method 任务详情
         * @description 描述一下方法的作用
         * @date: 2020/6/21 15:16
         * @author: wjy
         * @param:taskId 任务id
         */
        @POST("task/detail")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<TaskInfo>> taskDetail(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @return
         * @method 查询任务类型
         * @description 描述一下方法的作用
         * @date: 2020/6/21 15:18
         * @author: wjy
         * @param： token
         */
        @NetMethod(parameterNames = {"token"}, url = "task/typeList")
        Observable<TypeResult> taskTypeList(String token);

        /**
         * @return
         * @param:token
         * @method
         * @description 查询状态列表
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @NetMethod(parameterNames = {"token"}, url = "task/statusList")
        Observable<NewStatusList> taskStatusList(String token);

        /**
         * @param:token
         * @returnS
         * @method
         * @description 查询提醒方式状态列表
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @NetMethod(parameterNames = {"token"}, url = "task/remindWayList")
        Observable<RemindWayResult> taskRemindWayList(String token);

        /**
         * @param:token
         * @returnS
         * @method
         * @description 新增任务
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("task/add")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<Map<String, String>> taskAdd(@Body RequestBean<TaskAddInfo> requestBean);

        /**
         * @param:token taskId 任务ID status 任务状态
         * @returnS
         * @method
         * @description 更新任务状态
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("task/modifyStatus")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<String>> taskModifyStatus(@Body RequestBean<Map<String, String>> requestBean);
    }

    /**
     * 客户
     */

    public interface Customer {
        /**
         * @param:token taskId 任务ID status 任务状态
         * @returnS
         * @method
         * @description 获取客户列表
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("customer/list")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<CustomerReiveInfo>> customerList(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param:token customerId 客户ID
         * @returnS
         * @method
         * @description 获取客户详情
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("customer/detail")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<CustomerInfo>> customerDetail(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param:token CustomerINfo
         * @returnS
         * @method
         * @description 增加客户
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("customer/add")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<String>> customerAdd(@Body RequestBean<CustomerInfo> requestBean);

        /**
         * @param:token CustomerINfo
         * @returnS
         * @method
         * @description 编辑用户
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("customer/modify")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<String>> customerModify(@Body RequestBean<CustomerInfo> requestBean);

        /**
         * @param:token customerId 客户ID
         * @returnS
         * @method
         * @description 转移公海
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("customer/belongPublic")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<String> customerBelongPublic(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param:token
         * @returnS
         * @method
         * @description 5.6查询客户分组列表
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @NetMethod(parameterNames = {"token"}, url = "customer/groupList")
        Observable<GroupResult> customerGroupList(String token);

        /**
         * @param:token
         * @returnS
         * @method
         * @description 5.7查询客户标签列表
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @NetMethod(parameterNames = {"token"}, url = "customer/tagList")
        Observable<TagsInfo> customerTagList(String token);

        /**
         * @param:token customerId 客户ID
         * @returnS
         * @method
         * @description 5.8查询客户分配历史列表
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("customer/belongList")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<List<BelongUserInfo>>> customerBelongList(@Body RequestBean<Map<String, String>> requestBean);

    }

    /**
     * 订单
     */
    public interface IOrder {

        /**
         * @param:{ "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         * "data":{
         * "pageNo":"1",
         * "pageSize":"10",
         * "orderName":"网络设备",
         * "orderSerialNumber":"2020060112342800000001",
         * "orderStatus":"1",
         * "customerName":"张三",
         * "orderBeginDate":"2020-01-01",
         * "orderEndDate":"2020-01-31"
         * }
         * }
         * @returnS
         * @method
         * @description 6.1查询订单列表
         * @date: 2020/6/21 15:22
         * @author: wjy
         */
        @POST("order/list")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<OrderListInfo>> orderList(@Body RequestBean<RequestOrder> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "orderId":"1238973749903737840"
         *          }
         *          }
         * @return
         * @method 6.2查询订单详情
         * @description 描述一下方法的作用
         * @date: 2020/6/26 19:16
         * @author: wjy
         */
        @POST("order/detail")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<OrderDetailInfo>> orderDetail(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "orderId":"1238973749903737840"
         *          }
         *          }
         * @return
         * @method 6.3查询订单商品记录
         * @description 描述一下方法的作用
         * @date: 2020/6/26 19:22
         * @author: wjy
         */
        @POST("order/goodsList")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<GoodListInfo>> orderGoodsList(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b",
         *          "data":{
         *          "orderId":"1238973749903737840"
         *          }
         *          }
         * @return
         * @method
         * @description 6.4查询订单回款记录
         * @date: 2020/6/26 19:26
         * @author: wjy
         */
        @POST("order/receivedPaymentsList")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<ReceivePayListInfo>> orderReceivedPaymentsList(@Body RequestBean<Map<String, String>> requestBean);

        /**
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b"
         * @return
         * @method
         * @description 6.5查询订单状态列表
         * @date: 2020/6/26 19:31
         * @author: wjy
         */
        @NetMethod(parameterNames = {"token"},url = "order/statusList")
        Observable<StatusList> orderStatusList(String token);
         /**
          * @method
          * @description 6.6查询支付方式列表
          * @date: 2020/6/26 19:32
          * @author: wjy
          * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b"
          * @return 
          */
        @NetMethod(parameterNames = {"token"}, url = "order/payWayList")
        Observable<PayWayResult> orderPayWayList(String token);
        /**
         * @method
         * @description 6.7查询商品列表
         * @date: 2020/6/26 19:32
         * @author: wjy
         * @param { "token":"a9c183ecfc57d8ebfcd04748e1561b0b"
         * @return
         */
        @POST("goods/list")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<GoodsList>>goodsList(@Body RequestBean<Map<String, String>> requestBean);
         /**
          * @method
          * @description 6.8新增订单
          * @date: 2020/6/26 19:42
          * @author: wjy
          * @param
          * @return 
          */
        @POST("order/add")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<Map<String,String>>> orderAdd(@Body RequestBean<AddOrderInfo> params);
        /**
         * @method
         * @description 6.9编辑订单
         * @date: 2020/6/26 19:42
         * @author: wjy
         * @param
         * @return
         */
        @POST("order/modify")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<Map<String,String>>> orderModify(@Body RequestBean<AddOrderInfo> params);
   /**
    * @method
    * @description 6.10新增订单回款记录
    * @date: 2020/6/26 19:50
    * @author: wjy
    * @param 
    * @return 
    */
        @POST("order/receivedPaymentAdd")
        @Headers({"Content-Type: application/json", "Accept: application/json"})
        Observable<ApiResult<Map<String,String>>> orderReceivedPaymentAdd(@Body RequestBean<Map<String,String>> params);
    }
  public static void downfile(String url){
      XHttp.downLoad(url)
              .savePath(PathUtils.getAppIntFilesPath())
              .execute(new DownloadProgressCallBack<String>() {
                  @Override
                  public void onStart() {
                      HProgressDialogUtils.showHorizontalProgressDialog(getContext(), getString(R.string.downLoading), true);
                  }

                  @Override
                  public void onError(ApiException e) {
                      ToastUtils.toast(e.getMessage());
                      HProgressDialogUtils.cancel();
                  }

                  @Override
                  public void update(long bytesRead, long contentLength, boolean done) {
                      HProgressDialogUtils.onLoading(contentLength, bytesRead); //更新进度条
                  }

                  @Override
                  public void onComplete(String path) {
                      ToastUtils.toast("下载成功, 保存路径:" + path);
                      HProgressDialogUtils.cancel();
                  }
              });
  }

}
